var app = getApp();
var create_course_order = require('../../../config').create_course_order; //创建订单
var order_sgin = require('../../../config').order_sgin; //获取微信支付签名

Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconColor_a: "#06af32",
    iconColor_b: "#dedede",
    cprice: "",
    cname: "",
    tname: "",
    cid: "", //课程ID
    paytype: '5',   //支付方式：5小程序支付
    orderid: '', //订单ID
    ordertype: '0', //订单类型，课程0，教室1，会员卡2， 余额3，商品4
    openid: '',
    iPhoneHintView: false,
    iHintText: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      cprice: options.cprice,
      cname: options.cname,
      tname: options.tname,
      cid: options.cid,
    })
  },

  /**
   * 支付方式选择
   */
  upPayMethod: function (res) {
    if (res.currentTarget.dataset.ptype == 'wx') {
      this.setData({
        iconColor_a: "#06af32",
        iconColor_b: "#dedede",
        paytype: '5'
      })
    }
    if (res.currentTarget.dataset.ptype == 'yue') {
      this.setData({
        iconColor_a: "#dedede",
        iconColor_b: "#06af32",
        paytype: '3',
      })
    }
  },

  /**
   * 事件绑定，发起支付获取支付订单
   */
  launchPayMethod: function (res) {
    var that = this;
    wx.request({
      url: create_course_order,
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "yamon-com-uid": app.data.uid,
        "yamon-com-token": app.data.token,
      },
      data: {
        vid: app.data.vid,
        cid: that.data.cid,
        paytype: that.data.paytype,
      },
      success: function (res) {
        if (res.data.code == 200 || res.data.code == 407 || res.data.code == 408 || res.data.code == 409 || res.data.code == 401) {
          if (res.data.code == 200) {
            that.setData({
              orderid: res.data.extra,
            })
            that.getWeixinPayParameter();
          }
          else if (res.data.code == 407) {
            that.showiPhoneHintInterac(true, res.data.msg);
          }
          else if (res.data.code == 408) {
            that.showiPhoneHintInterac(true, res.data.msg);
          }
          else if (res.data.code == 409) {
            that.showiPhoneHintInterac(true, res.data.msg);
          }
          else if (res.data.code == 401) {
            that.showiPhoneHintInterac(true, '登录toekn过期');
          }
        }
        else {
          that.showiPhoneHintInterac(true, '请稍后重试');
        }
      },
      fail: function (res) {
        that.showiPhoneHintInterac(true, '请稍后重试');
      },
    })
  },

  /**
   * 获取微信支付参数
   */
  getWeixinPayParameter: function () {
    var that = this;
    wx.request({
      url: order_sgin,
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "yamon-com-uid": app.data.uid,
        "yamon-com-token": app.data.token,
      },
      data: {
        openid: app.data.openid,
        orderid: that.data.orderid,
        ordertype: that.data.ordertype,
        vid: app.data.vid,
        paytype: that.data.paytype,
        key: app.data.key,
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.launchWeixinPayMethod(res.data.extra);
        }
      },
      fail: function (res) {

      }
    })
  },

  /**
   * 发起微信支付
   */
  launchWeixinPayMethod: function (res) {
    wx.requestPayment({
      timeStamp: res.timeStamp,  //时间戳
      nonceStr: res.nonceStr,    //随机字符串
      package: res.package,      //统一下单接口返回的 prepay_id 参数值
      signType: 'MD5',           //签名算法，暂支持 MD5
      paySign: res.paySign,      //签名
      success: function (res) {
        wx.setStorageSync('payProject', true);
        wx.navigateBack({});
      },
      fail: function (res) {
        if (res.errMsg === 'requestPayment:fail cancel') {
          if (wx.canIUse('showToast.image')) {
            wx.showToast({
              title: '支付取消',
              image: "../../../img/payfail.png",
            })
          }
          else {
            wx.showToast({
              title: '支付取消',
              icon: 'success',
            })
          }
        }
        else{
          if (wx.canIUse('showToast.image')) {
            wx.showToast({
              title: '支付失败',
              image: "../../../img/payfail.png",
            })
          }
          else {
            wx.showToast({
              title: '支付失败',
              icon: 'success',
            })
          }
        }
      },
      complete: function (res) {
        if (res.errMsg === 'requestPayment:fail cancel'){
          if (wx.canIUse('showToast.image')) {
            wx.showToast({
              title: '支付取消',
              image: "../../../img/payfail.png",
            })
          }
          else {
            wx.showToast({
              title: '支付取消',
              icon: 'success',
            })
          }
        }
      },
    })
  },

  /**
   * 设置手机提示
   */
  showiPhoneHintInterac: function (state, txt) {
    var that = this;
    that.setData({
      iPhoneHintView: state,
      iHintText: txt,
    })
    setTimeout(function () {
      that.setData({
        iPhoneHintView: false,
        iHintText: '',
      })
    }, 1000)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})